package com.zjzywl.smoke.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.config.BaseDataConfig;
import com.zjzywl.smoke.repository.AlarmRepository;
import com.zjzywl.smoke.repository.DeviceRepository;
import com.zjzywl.smoke.repository.DeviceStatusRepository;
import com.zjzywl.smoke.repository.SysUserRepository;
import com.zjzywl.smoke.util.MQWebSocket;
import com.zjzywl.smoke.util.WebResult;
import com.zjzywl.smoke.entity.Alarm;
import com.zjzywl.smoke.entity.SysUser;

@RestController
@RequestMapping(value = "/androidAlarm")
public class AndroidAlarmController extends BaseController {
	@Autowired
	private AlarmRepository repository;
	
	@Autowired
	private SysUserRepository userRepository;

	@Autowired
	private DeviceRepository deviceRepository;

	@Autowired
	private DeviceStatusRepository deviceStatusRepository;

	// 获取未处理的当前用户的烟感火警
	@RequestMapping(value = "/smoke/byuser")
	 
	public WebResult getNotDisposeSmokeAlarmByUser(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		Integer status = Integer.parseInt(request.getParameter("status"));
		SysUser user = userRepository.findByLoginName(loginName);

		List<Alarm> alarms = repository.getByDisposeStatusAndAlarmTypeAndUser(user.getUserId().toString(), status,
				BaseDataConfig.ALARM_FIRE);

		return WebResult.OK(alarms);
	}

	@RequestMapping(value = "/smoke/dealtype")
	 
	public WebResult getNotDisposeSmokeAlarm(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		Integer status = 0;
		SysUser user = userRepository.findByLoginName(loginName);

		List<Alarm> alarms = repository.getByDisposeStatusAndAlarmType(user.getUserId().toString(), status,
				BaseDataConfig.ALARM_FIRE);

		return WebResult.OK(alarms);
	}

	// 获取未处理的当前用户的阴井盖位移报警
	@RequestMapping(value = "/humiture/byuser")
	 
	public WebResult getNotDisposeHumitureAlarmByUser(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		SysUser user = userRepository.findByLoginName(loginName);
		List<Alarm> alarms = repository.getByDisposeStatusAndAlarmTypeAndUser(user.getUserId().toString(), 0,
				BaseDataConfig.ALARM_Heat);
		return WebResult.OK(alarms);
	}

	// 报警历史记录查询 新接口--返回数据精简
	@GetMapping(value = "/historylist")
	 
	public WebResult getAlarmSimple(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		String alarmTypeCode = request.getParameter("alarmTypeCode");
		SysUser user = userRepository.findByLoginName(loginName);
		List<Alarm> alarms = repository.getAlarmByalarmTypeId(user.getUserId(), alarmTypeCode);

		return WebResult.OK(alarms);
	}
	

	// 处理报警
	@PostMapping(value = "/data/disposealarm")
	 
	public WebResult disposeAlarm(@PathParam("alarmId") Integer alarmId,
			@PathParam("disposeStatus") Integer disposeStatus, @PathParam("username") String username)
			throws IOException {
		Alarm alarm = repository.findOne(alarmId);
		SysUser user = userRepository.findByLoginName(username);
		String name = null;
		if (user != null) {
			name = user.getName();
		} else {
			System.out.println("not find user:" + username);
		}
		if (alarm != null) {
			System.out.println("收到报警处理请求");

			alarm.setDisposeStatus(disposeStatus);
			if (disposeStatus == 1) {
				alarm.setDisposeContent("处理");
			} else {
				alarm.setDisposeContent("忽略");
			}
			alarm.setDisposeTime(new Date());
			alarm.setDisposePeople(name);
			repository.save(alarm);
			MQWebSocket.sendInfo(BaseDataConfig.CMD_ALARMDISPOSESYNC + "," + alarm.getAlarmId());
			return WebResult.OK(name);
		} else {
			return WebResult.FAIL("not find alarm:" + alarmId);
		}
	}

	@RequestMapping(value = "/data/dealalarm")
	 
	public WebResult disposeAlarms(@PathParam("alarmId") Integer alarmId,
			@PathParam("disposeStatus") Integer disposeStatus) throws IOException {
		Alarm alarm = repository.findOne(alarmId);
		if (alarm != null) {
			System.out.println("收到报警处理请求");

			alarm.setDisposeStatus(disposeStatus);
			if (disposeStatus == 1) {
				alarm.setDisposeContent("处理");
			} else {
				alarm.setDisposeContent("忽略");
			}
			alarm.setDisposeTime(new Date());
			repository.save(alarm);
			MQWebSocket.sendInfo(BaseDataConfig.CMD_ALARMDISPOSESYNC + "," + alarm.getAlarmId());
			return WebResult.OK();
		} else {
			return WebResult.FAIL("not find alarm:" + alarmId);
		}
	}

	// 统计设备数量
	@RequestMapping(value = "/count/bydevicetype")
	 
	public WebResult getCountByDeviceTypeAndUser(HttpServletRequest request) {
		String loginName = request.getParameter("loginName");
		String devicetypeid = request.getParameter("devicetypeid");
		SysUser user = userRepository.findByLoginName(loginName);
		String count = deviceRepository.getDeviceCountByUserAndDeviceType(user.getUserId().toString(), devicetypeid)
				.get(0).toString();
		try {
			return WebResult.OK(Integer.valueOf(count));
		} catch (Exception e) {
			return WebResult.FAIL(e.getMessage());
		}
	}

	// 报警历史记录查询 新接口
	@GetMapping(value = "/data/history/list")
	 
	public WebResult getAlarm(@PathParam("loginName") String loginName, @PathParam("beginTime") String beginTime,
			@PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
			@PathParam("deviceTypeId") String deviceTypeId, @PathParam("address") String address) {
		SysUser user = userRepository.findByLoginName(loginName);
		List alarms = repository.getAlarmByTimeAndAlarmTypeAndAddressAndUser(beginTime, endTime, alarmTypeCode,
				deviceTypeId, address, user.getUserId() + "");

		return WebResult.OK(alarms);
	}
	// 按照时间段统计重点关注设备
	@RequestMapping(value = "/data/device/count")
	 
	public WebResult getDeviceAlarmCountEx(@PathParam("beginTime") String beginTime,
			@PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
			@PathParam("deviceTypeId") String deviceTypeId,@PathParam("loginName") String loginName) {
		String bTime = beginTime;
		String eTime = endTime;
		SysUser user = userRepository.findByLoginName(loginName);
		String count = appConfig.getFocusMoveByCount();
		String userId =user.getUserId()+"";
		if (count.length() > 0) {
			List alarms = repository.getDeviceAlarmCountByTimeEx(bTime, eTime, Integer.valueOf(count), alarmTypeCode,
					deviceTypeId, userId);
			return WebResult.OK(alarms);
		} else {
			return WebResult.FAIL("获取配置文件FocusMoveByCount信息出错！");
		}
	}
	
	// 按照时间段、组织统计一周内每天报警数量
		@RequestMapping(value = "/data/org/device/count")
		 
		public WebResult getOrgAlarmCountByTimeEx(@PathParam("beginTime") String beginTime,
				@PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
				@PathParam("deviceTypeId") String deviceTypeId,@PathParam("loginName") String loginName) {
			String bTime = beginTime;
			String eTime = endTime;
			SysUser user = userRepository.findByLoginName(loginName);
			String userId =user.getUserId()+"";

			List alarms = repository.getOrgAlarmCountByTimeEx(bTime, eTime, 0, alarmTypeCode, deviceTypeId, userId);
			return WebResult.OK(alarms);

		}
		/*
		 * 根据设备类型、状态、用户获取设备状态信息
		 */
		@RequestMapping(value = "/data/list/bystatus/{status}/bydevicetype/{devicetypeid}/{username}")
		 
		public WebResult getListByStatusAndDeviceTypeAndUser(@PathVariable("status") String status,
				@PathVariable("devicetypeid") String devicetypeid,@PathVariable("username") String username) {
			SysUser user = userRepository.findByLoginName(username);
			List ds = deviceStatusRepository.getDSByUserAndDeviceTypeAndStatus(user.getUserId().toString(),
					status, devicetypeid);
			try {
				return WebResult.OK(ds);
			} catch (Exception e) {
				// TODO: handle exception
				return WebResult.FAIL(e.getMessage());
			}
		}
}
